﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VCC.Lib.Paging;
using System.Linq.Dynamic;
using VCC.Lib.Common;
namespace VCC.Model
{
    public class ArticleManager
    { 
        public static ArticleCategory GetCategory(int catId)
        {
            var dataContext = new ChoVietDataContext();
            return dataContext.ArticleCategories.SingleOrDefault(c => c.ID == catId);
        }
        public static List<int> GetChild(int catid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<int>();
                var curentCat = db.ArticleCategories.Single(o => o.ID == catid);
                returnVal.Add(curentCat.ID);

                foreach (var item in curentCat.ArticleCategories)
                {
                    returnVal.AddRange(GetChild(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }
        public static List<ArticleCategory> GetChildCategory(int catid)
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<ArticleCategory>();
                var curentCat = db.ArticleCategories.Single(o => o.ID == catid);
                returnVal.Add(curentCat);
                foreach (var item in curentCat.ArticleCategories)
                {
                    returnVal.AddRange(GetChildCategory(item.ID));
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public static List<ArticleCategory> GetListCategory()
        {
            try
            {
                var db = new ChoVietDataContext();
                var returnVal = new List<ArticleCategory>();
                var listParent = db.ArticleCategories.Where(e => e.ParentID == null).OrderBy(string.Format("{0} {1}", "Priority", "asc")).ToList();
                foreach (var item in listParent)
                {
                    var l = GetChildCategory(item.ID);
                    returnVal.AddRange(l);
                }

                return returnVal;
            }
            catch (Exception exp)
            {
                return null;
            }
        }

        public static Article GetArticle(int articleId)
        {
            var db = new ChoVietDataContext();
            return db.Articles.SingleOrDefault(a => a.ID == articleId && a.PubYN == true);
        }

        public static IPagedList<Article> GetArticles(int catID,int languageid, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new ChoVietDataContext();
            var listcat = catID > 0 ? GetChild(catID) : new List<int>(); 
            var articles = dataContext.Articles
                .Where(a => catID > 0 ? listcat.ToArray().Contains(a.CatID.Value) : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                //.Where(a => a.TypeID == ArticleListType.NormalArticle)
                .Where(a => a.LanguageID == languageid)
                .Where(a => keyword==string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = articles.ToPagedList(page, pageSize);
            return returnVal;
        }

        public static IList<Article> GetList(int catID, string keyword, int hotYN, int pubYN, string order, string orderDir, int number, int skip)
        {
            return GetList(catID, keyword, hotYN, pubYN, 0, order, orderDir, number, skip);
        }

        public static IList<Article> GetList(int catID, string keyword, int hotYN, int pubYN,int vipYN, string order, string orderDir, int number, int skip)
        {
            var dataContext = new ChoVietDataContext();
            var listcat = catID > 0 ? GetChild(catID) : new List<int>();
            var l = dataContext.Articles
                .Where(a => catID > 0 ? listcat.ToArray().Contains(a.CatID.Value) : true)
                .Where(a => hotYN > 0 ? a.HotYN == true : true)
                .Where(a => vipYN > 0 ? a.VipYN == true : true)
                .Where(a => pubYN > 0 ? a.PubYN == true : true)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var rt = l.Skip(skip).Take(number).ToList();
            return rt;
        }
        
        /// <summary>
        /// Created by Vietlv
        /// Description: Get list articles by catID, Guide. Include sub articles in category
        /// Created date: 15/09/2011
        /// </summary>
        /// <returns></returns>
        public static IPagedList<Article> GetArticles(int catID, Guid addBy, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new ChoVietDataContext();
            var listcat = catID > 0 ? GetChild(catID) : new List<int>();
            var articles = dataContext.Articles
                .Where(a => addBy != null ? a.AddBy == addBy : true)
                .Where(a => catID > 0 ? listcat.ToArray().Contains(a.CatID.Value) : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(a => keyword == string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = articles.ToPagedList(page, pageSize);
            return returnVal;
        }

        /*
        /// <summary>
        /// Created by Vietlv
        /// Description: Get list articles by CompanyID, Guide. Include sub articles in category
        /// Created date: 15/09/2011
        /// </summary>
        /// <returns></returns>
        public static IPagedList<Article> GetArticlesByCompany(int catID,int companyID, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        { 
            var dataContext = new ChoVietDataContext();
            var listcat = catID > 0 ? GetChild(catID) : new List<int>();
            var listIDArticle = dataContext.ArticleCompanyMaps.Where(e => e.CompanyID == companyID).Select(e => e.ArticleID).ToList();
                
            var articles = dataContext.Articles
                .Where(a => catID > 0 ? listcat.ToArray().Contains(a.CatID.Value) : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(e => listIDArticle != null && listIDArticle.Count() > 0 ? listIDArticle.Contains(e.ID) : false )
                .Where(a => keyword==string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = articles.ToPagedList(page, pageSize);
            return returnVal;
        }
         */

        /*
        public static IPagedList<Article> GetArticles(int catID, Guid? addBy,int companyid, string keyword, int hotYN, int pubYN, string order, string orderDir, int page, int pageSize)
        {
            var dataContext = new ChoVietDataContext();
            var listcat = catID > 0 ? GetChild(catID) : new List<int>();
            var articles = dataContext.Articles
                .Where(a => addBy != null ? a.AddBy == addBy : true)
                .Where(a=>companyid>0? a.ArticleCompanyMaps.Where(e=>e.CompanyID==companyid).ToList().Count>0:true)
                .Where(a => catID > 0 ? listcat.ToArray().Contains(a.CatID.Value) : true)
                .Where(a => hotYN > 0 ? a.HotYN == (hotYN == 1 ? true : false) : true)
                .Where(a => pubYN > 0 ? a.PubYN == (pubYN == 1 ? true : false) : true)
                .Where(a => keyword == string.Empty ? true : (a.Title.Contains(keyword.ToLower()) || (a.Summary.ToLower().Contains(keyword.ToLower()))))
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var returnVal = articles.ToPagedList(page, pageSize);
            return returnVal;
        }
         */

        /*
        public static IList<Article> GetOtherList(int id, string keyword, int hotYN, int pubYN, string order, string orderDir, int number, int skip, int projectID, int companyYN, int eventid)
        {
            var dataContext = new ChoVietDataContext();
            var c = dataContext.Articles.Where(e => e.ID == id).SingleOrDefault();
            var com = companyYN > 0 ? c.ArticleCompanyMaps.FirstOrDefault() : new ArticleCompanyMap();
            var listcat = c.ArticleCategory != null ? GetChild(c.ArticleCategory.ID) : new List<int>();
            var l = dataContext.Articles
                .Where(a => listcat.Count > 0 && eventid<=0 ? listcat.ToArray().Contains(a.CatID.Value) : true)
                .Where(a => hotYN > 0 ? a.HotYN == true : true)
                .Where(a => pubYN > 0 ? a.PubYN == true : true)
                .Where(a=>a.ID!=id)
                .Where(a=>projectID >0 ? a.ArticleProjectMaps.Where(e=>e.ProjectID==projectID).ToList().Count>0:true)
                .Where(a => companyYN > 0 ?(com !=null? a.ArticleCompanyMaps.Where(e => e.CompanyID == com.CompanyID).ToList().Count > 0: false) : true)
                .Where(a => eventid > 0 ? a.ArticleEventMaps.Where(e => e.EventID == eventid).ToList().Count > 0 : true)
                .OrderBy(string.Format("{0} {1}", order, orderDir)).AsQueryable();
            var rt =l!=null && l.ToList().Count>0 ? l.Skip(skip).Take(number).ToList(): new List<Article>();
            return rt;
        }
         */
        /*
        public static IList<Comment> GetComments(int id, bool? publishYN, int number)
        {
            var dataContext = new ChoVietDataContext();
            var l = (from c in dataContext.Comments
                     from m in dataContext.CommentsInArticles
                     where c.ID == m.CommentID && m.ArticleID == id && (publishYN == null ? true : c.PublishYN == publishYN)
                     orderby c.ID ascending
                     select c).AsQueryable();
            var returnVal = number > 0 ? l.Take(number).ToList() : l.ToList();
            return returnVal;
        }
         */ 
    }
}
